using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Globalization;
using Code5Systems.FormMaster.Business;
using Microsoft.ApplicationBlocks.Data;

namespace Code5Systems.FormMaster.Data
{

	public class SqlDataProvider : DataProvider
	{
		
#region Private Members
		
		private const string ProviderType = "data";
		
		private DotNetNuke.Framework.Providers.ProviderConfiguration _providerConfiguration; // VBConversions Note: Initial value of "DotNetNuke.Framework.Providers.ProviderConfiguration.GetProviderConfiguration(ProviderType)" cannot be assigned here since it is non-static.  Assignment has been moved to the class constructors.
		private string _connectionString;
		private string _providerPath;
		private string _objectQualifier;
		private string _databaseOwner;
		private const string ModuleQualifier = "";
		
#endregion
		
#region Constructors
		
		public SqlDataProvider()
		{
			_providerConfiguration = DotNetNuke.Framework.Providers.ProviderConfiguration.GetProviderConfiguration(ProviderType);

			// Read the configuration specific information for this provider
			DotNetNuke.Framework.Providers.Provider objProvider = (DotNetNuke.Framework.Providers.Provider) (_providerConfiguration.Providers[_providerConfiguration.DefaultProvider]);
			
			// Read the attributes for this provider
			if (objProvider.Attributes["connectionStringName"] != "" && System.Configuration.ConfigurationManager.ConnectionStrings[objProvider.Attributes["connectionStringName"]].ConnectionString != "")
			{
				_connectionString = System.Configuration.ConfigurationManager.ConnectionStrings[objProvider.Attributes["connectionStringName"]].ConnectionString;
			}
			else
			{
				_connectionString = objProvider.Attributes["connectionString"];
			}
			
			_providerPath = objProvider.Attributes["providerPath"];
			
			_objectQualifier = objProvider.Attributes["objectQualifier"];
			if (_objectQualifier != "" && _objectQualifier.EndsWith("_") == false)
			{
				_objectQualifier += "_";
			}
			
			_databaseOwner = objProvider.Attributes["databaseOwner"];
			if (_databaseOwner != "" && _databaseOwner.EndsWith(".") == false)
			{
				_databaseOwner += ".";
			}
			
		}
		
#endregion
		
#region Properties
		
		public override string ConnectionString
		{
			get
			{
				return _connectionString;
			}
		}
		
		public string ProviderPath
		{
			get
			{
				return _providerPath;
			}
		}
		
		public override string ObjectQualifier
		{
			get
			{
				return _objectQualifier;
			}
		}
		
		public string DatabaseOwner
		{
			get
			{
				return _databaseOwner;
			}
		}
		
#endregion
		
#region Private Methods
		private string GetFullyQualifiedName(string name)
		{
			return DatabaseOwner + ObjectQualifier + ModuleQualifier + name;
		}
		
		private object GetNull(object Field)
		{
			return DotNetNuke.Common.Utilities.Null.GetNull(Field, DBNull.Value);
		}
#endregion
		
#region Form Methods
		public override IDataReader GetForm(int moduleId)
		{
			return ((IDataReader) (SqlHelper.ExecuteReader(ConnectionString, DatabaseOwner + ObjectQualifier + "C5SLLC_FormGet", moduleId)));
		}
		
		public override IDataReader ListForm()
		{
			return ((IDataReader) (SqlHelper.ExecuteReader(ConnectionString, DatabaseOwner + ObjectQualifier + "C5SLLC_FormList")));
		}
		
		public override int AddForm(int portalID, int moduleID, string fromName, byte emailFromAction, string fromAddress, string emailSendFromControl, string bccAddress, byte emailSendAction, string emailSendTo, string emailSendToControl, bool saveData, byte tableColumns, byte tableRows, string tableClass, byte labelPosition, bool showCcMe, string emailCcMeControl, string connString, string formQuery, bool useCaptcha, string captchaCharacters, bool useExtTable, string ExtIdentityColumn, string extTableConnStr, string extTableName, byte replyToAction, string replyTo, string replyToControl, string formDefinition, int createdByID, bool ExtTableForEachPage)
		{
			return System.Convert.ToInt32(SqlHelper.ExecuteScalar(ConnectionString, DatabaseOwner + ObjectQualifier + "C5SLLC_FormAdd", portalID, moduleID, fromName, emailFromAction, fromAddress, GetNull(emailSendFromControl), GetNull(bccAddress), emailSendAction, GetNull(emailSendTo), GetNull(emailSendToControl), saveData, tableColumns, tableRows, GetNull(tableClass), labelPosition, showCcMe, emailCcMeControl, GetNull(connString), GetNull(formQuery), GetNull(useCaptcha), GetNull(captchaCharacters), GetNull(useExtTable), GetNull(ExtIdentityColumn), GetNull(extTableConnStr), GetNull(extTableName), GetNull(replyToAction), GetNull(replyTo), GetNull(replyToControl), formDefinition, createdByID, ExtTableForEachPage));
		}
		
		public override void UpdateForm(int moduleID, string formName, byte emailFromAction, string fromAddress, string emailSendFromControl, string bccAddress, byte emailSendAction, string emailSendTo, string emailSendToControl, bool saveData, byte tableColumns, byte tableRows, string tableClass, byte labelPosition, bool showCcMe, string emailCcMeControl, string connString, string formQuery, bool useCaptcha, string captchaCharacters, bool useExtTable, string ExtIdentityColumn, string extTableConnStr, string extTableName, byte replyToAction, string replyTo, string replyToControl, string formDefinition, int lastModifiedByID, bool ExtTableForEachPage)
		{
			
			SqlHelper.ExecuteNonQuery(ConnectionString, DatabaseOwner + ObjectQualifier + "C5SLLC_FormUpdate", moduleID, formName, emailFromAction, fromAddress, GetNull(emailSendFromControl), GetNull(bccAddress), emailSendAction, GetNull(emailSendTo), GetNull(emailSendToControl), saveData, tableColumns, tableRows, GetNull(tableClass), labelPosition, showCcMe, emailCcMeControl, GetNull(connString), GetNull(formQuery), GetNull(useCaptcha), GetNull(captchaCharacters), GetNull(useExtTable), GetNull(ExtIdentityColumn), GetNull(extTableConnStr), GetNull(extTableName), GetNull(replyToAction), GetNull(replyTo), GetNull(replyToControl), formDefinition, lastModifiedByID, ExtTableForEachPage);
		}
		
		public override void DeleteForm(int formID)
		{
			SqlHelper.ExecuteNonQuery(ConnectionString, DatabaseOwner + ObjectQualifier + "C5SLLC_FormDelete", formID);
		}
		
		public override void UpdateFormDefinition(int moduleId, string formDefinition, int lastModifiedById)
		{
			SqlHelper.ExecuteNonQuery(ConnectionString, DatabaseOwner + ObjectQualifier + "C5SLLC_FormUpdateDef", moduleId, formDefinition, lastModifiedById);
		}
		
		public override bool HasUserPosted(int moduleId, int userId)
		{
			int intRetVal = default(int);
			bool bHasPosted = false;
			intRetVal = System.Convert.ToInt32(SqlHelper.ExecuteScalar(ConnectionString, DatabaseOwner + ObjectQualifier + "C5SLLC_FormHasUserPosted", moduleId, userId));
			
			if (intRetVal > 0)
			{
				bHasPosted = true;
			}
			
			return bHasPosted;
			
		}
#endregion
		
#region FormData Methods
		public override IDataReader GetFormData(int formDataID, int moduleId)
		{
			return ((IDataReader) (SqlHelper.ExecuteReader(ConnectionString, DatabaseOwner + ObjectQualifier + "C5SLLC_FormDataGet", formDataID, moduleId)));
		}
		
		public override IDataReader ListFormData(int moduleId, DateTime dtStart, DateTime dtEnd, int pageNum, int pageSize)
		{
			return ((IDataReader) (SqlHelper.ExecuteReader(ConnectionString, DatabaseOwner + ObjectQualifier + "C5SLLC_FormDataList", moduleId, dtStart, dtEnd, pageNum, pageSize)));
		}
		
		public override IDataReader ListFormDataExt(int moduleId, DateTime dtStart, DateTime dtEnd, int pageNum, int pageSize)
		{
			return ((IDataReader) (SqlHelper.ExecuteReader(ConnectionString, DatabaseOwner + ObjectQualifier + "C5SLLC_FormDataListExt", moduleId, dtStart, dtEnd, pageNum, pageSize)));
		}
		
		public override int AddFormData(int formID, int portalID, int moduleID, string formData, int extTableRowId, int createdByID, SqlTransaction objTransaction = null)
		{
			if (objTransaction == null)
			{
				return System.Convert.ToInt32(SqlHelper.ExecuteScalar(ConnectionString, DatabaseOwner + ObjectQualifier + "C5SLLC_FormDataAdd", formID, portalID, moduleID, formData, extTableRowId, createdByID));
			}
			else
			{
				return System.Convert.ToInt32(SqlHelper.ExecuteScalar(objTransaction, DatabaseOwner + ObjectQualifier + "C5SLLC_FormDataAdd", formID, portalID, moduleID, formData, extTableRowId, createdByID));
			}
		}
		
		public override void UpdateFormData(int formDataId, int formID, int portalID, int moduleID, string formData, int extTableRowId, int lastModifiedByID, SqlTransaction objTransaction = null)
		{
			if (objTransaction == null)
			{
				SqlHelper.ExecuteNonQuery(ConnectionString, DatabaseOwner + ObjectQualifier + "C5SLLC_FormDataUpdate", formDataId, formID, portalID, moduleID, formData, extTableRowId, lastModifiedByID);
			}
			else
			{
				SqlHelper.ExecuteNonQuery(objTransaction, DatabaseOwner + ObjectQualifier + "C5SLLC_FormDataUpdate", formDataId, formID, portalID, moduleID, formData, extTableRowId, lastModifiedByID);
			}
		}
		
		public override void DeleteFormDataRange(int moduleId, DateTime dtStart, DateTime dtEnd, SqlTransaction objTransaction = null)
		{
			if (objTransaction == null)
			{
				SqlHelper.ExecuteNonQuery(ConnectionString, DatabaseOwner + ObjectQualifier + "C5SLLC_FormDataDeleteRange", moduleId, dtStart, dtEnd);
			}
			else
			{
				SqlHelper.ExecuteNonQuery(objTransaction, DatabaseOwner + ObjectQualifier + "C5SLLC_FormDataDeleteRange", moduleId, dtStart, dtEnd);
			}
			
		}
		
		public override IDataReader GetExtIdByDate(int moduleId, DateTime dtStart, DateTime dtEnd)
		{
			return ((IDataReader) (SqlHelper.ExecuteReader(ConnectionString, DatabaseOwner + ObjectQualifier + "C5SLLC_FormDataExtIdByDate", moduleId, dtStart, dtEnd)));
		}
		
		public override void DeleteFormData(int moduleId, int formDataId, SqlTransaction objTransaction = null)
		{
			if (objTransaction == null)
			{
				SqlHelper.ExecuteNonQuery(ConnectionString, DatabaseOwner + ObjectQualifier + "C5SLLC_FormDataDelete", moduleId, formDataId);
			}
			else
			{
				SqlHelper.ExecuteNonQuery(objTransaction, DatabaseOwner + ObjectQualifier + "C5SLLC_FormDataDelete", moduleId, formDataId);
			}
			
		}
		
		public override int AddExtTableData(string sql, List<FormMappingInfo> alMappings, string connString, CultureInfo userCulture)
		{
			SqlDbType sqlDataType = default(SqlDbType);
			SqlParameter[] sqlParams = new SqlParameter[alMappings.Count - 1 + 1];
			
			FormMappingInfo oMapping = default(FormMappingInfo);
			for (int index = 0; index <= alMappings.Count - 1; index++)
			{
				oMapping = alMappings[index];
				sqlParams[index] = new SqlParameter();
				switch (oMapping.DataType)
				{
					case "tinyint":
						sqlDataType = SqlDbType.TinyInt;
						if (!string.IsNullOrEmpty(oMapping.Value))
						{
							sqlParams[index].Value = System.Data.SqlTypes.SqlByte.Parse(oMapping.Value);
						}
						break;
					case "smallint":
						sqlDataType = SqlDbType.SmallInt;
						if (!string.IsNullOrEmpty(oMapping.Value))
						{
							sqlParams[index].Value = System.Data.SqlTypes.SqlInt16.Parse(oMapping.Value);
						}
						break;
					case "int":
						sqlDataType = SqlDbType.Int;
						if (!string.IsNullOrEmpty(oMapping.Value))
						{
							sqlParams[index].Value = System.Data.SqlTypes.SqlInt32.Parse(oMapping.Value);
						}
						break;
					case "bigint":
						sqlDataType = SqlDbType.BigInt;
						if (!string.IsNullOrEmpty(oMapping.Value))
						{
							sqlParams[index].Value = System.Data.SqlTypes.SqlInt64.Parse(oMapping.Value);
						}
						break;
					case "nchar":
						sqlDataType = SqlDbType.NChar;
						sqlParams[index].Size = oMapping.Length;
						sqlParams[index].Value = oMapping.Value;
						break;
					case "char":
						sqlDataType = SqlDbType.Char;
						sqlParams[index].Size = oMapping.Length;
						sqlParams[index].Value = oMapping.Value;
						break;
					case "nvarchar":
						sqlDataType = SqlDbType.NVarChar;
						sqlParams[index].Size = oMapping.Length;
						sqlParams[index].Value = oMapping.Value;
						break;
					case "varchar":
						sqlDataType = SqlDbType.VarChar;
						sqlParams[index].Size = oMapping.Length;
						sqlParams[index].Value = oMapping.Value;
						break;
					case "ntext":
						sqlDataType = SqlDbType.NText;
						sqlParams[index].Value = oMapping.Value;
						break;
					case "text":
						sqlDataType = SqlDbType.Text;
						sqlParams[index].Value = oMapping.Value;
						break;
					case "float":
						sqlDataType = SqlDbType.Float;
						if (!string.IsNullOrEmpty(oMapping.Value))
						{
							sqlParams[index].Value = System.Data.SqlTypes.SqlDouble.Parse(oMapping.Value);
						}
						break;
					case "real":
						sqlDataType = SqlDbType.Real;
						if (!string.IsNullOrEmpty(oMapping.Value))
						{
							sqlParams[index].Value = System.Data.SqlTypes.SqlSingle.Parse(oMapping.Value);
						}
						break;
					case "money":
						sqlDataType = SqlDbType.Money;
						if (!string.IsNullOrEmpty(oMapping.Value))
						{
							sqlParams[index].Value = System.Data.SqlTypes.SqlMoney.Parse(oMapping.Value);
						}
						break;
					case "smallmoney":
						sqlDataType = SqlDbType.SmallMoney;
						if (!string.IsNullOrEmpty(oMapping.Value))
						{
							sqlParams[index].Value = System.Data.SqlTypes.SqlMoney.Parse(oMapping.Value);
						}
						break;
					case "datetime":
						sqlDataType = SqlDbType.DateTime;
						if (!string.IsNullOrEmpty(oMapping.Value))
						{
							sqlParams[index].Value = DateTime.Parse(oMapping.Value, userCulture);
						}
						break;
					case "datetime2":
						sqlDataType = SqlDbType.DateTime2;
						if (!string.IsNullOrEmpty(oMapping.Value))
						{
							sqlParams[index].Value = DateTime.Parse(oMapping.Value, userCulture);
						}
						break;
					case "smalldatetime":
						sqlDataType = SqlDbType.SmallDateTime;
						if (!string.IsNullOrEmpty(oMapping.Value))
						{
							sqlParams[index].Value = DateTime.Parse(oMapping.Value, userCulture);
						}
						break;
					case "bit":
						sqlDataType = SqlDbType.Bit;
						if (!string.IsNullOrEmpty(oMapping.Value))
						{
							sqlParams[index].Value = System.Data.SqlTypes.SqlBoolean.Parse(oMapping.Value);
						}
						break;
					case "uniqueidentifier":
						sqlDataType = SqlDbType.UniqueIdentifier;
						if (!string.IsNullOrEmpty(oMapping.Value))
						{
							sqlParams[index].Value = System.Data.SqlTypes.SqlGuid.Parse(oMapping.Value);
						}
						break;
					case "decimal":
						sqlDataType = SqlDbType.Decimal;
						if (!string.IsNullOrEmpty(oMapping.Value))
						{
							sqlParams[index].Value = System.Data.SqlTypes.SqlDecimal.Parse(oMapping.Value);
						}
						break;
					case "numeric":
						sqlDataType = SqlDbType.Decimal;
						if (!string.IsNullOrEmpty(oMapping.Value))
						{
							sqlParams[index].Value = System.Data.SqlTypes.SqlDecimal.Parse(oMapping.Value);
						}
						break;
				}
				
				if (oMapping.Nullable)
				{
					if (string.IsNullOrEmpty(oMapping.Value))
					{
						sqlParams[index].Value = System.DBNull.Value;
					}
				}
				
				sqlParams[index].ParameterName = "@" + oMapping.FormField;
				sqlParams[index].SqlDbType = sqlDataType;
				sqlParams[index].Direction = ParameterDirection.Input;
				sqlParams[index].Precision = oMapping.Prec;
				sqlParams[index].Scale = oMapping.Scale;
				sqlParams[index].IsNullable = oMapping.Nullable;
				sqlParams[index].SourceColumn = oMapping.TableField;
			}
			
			if (string.IsNullOrEmpty(connString))
			{
				connString = this.ConnectionString;
			}
			
			return System.Convert.ToInt32(SqlHelper.ExecuteScalar(connString, CommandType.Text, sql, sqlParams));
			
		}
		
		public override void UpdateExternalTable(string sql, List<FormMappingInfo> alMappings, string connString, CultureInfo userCulture)
		{
			SqlDbType sqlDataType = default(SqlDbType);
			SqlParameter[] sqlParams = new SqlParameter[alMappings.Count - 1 + 1];
			FormMappingInfo oMapping = default(FormMappingInfo);
			for (int index = 0; index <= alMappings.Count - 1; index++)
			{
				oMapping = alMappings[index];
				sqlParams[index] = new SqlParameter();
				switch (oMapping.DataType)
				{
					case "tinyint":
						sqlDataType = SqlDbType.TinyInt;
						if (!string.IsNullOrEmpty(oMapping.Value))
						{
							sqlParams[index].Value = System.Data.SqlTypes.SqlByte.Parse(oMapping.Value);
						}
						break;
					case "smallint":
						sqlDataType = SqlDbType.SmallInt;
						if (!string.IsNullOrEmpty(oMapping.Value))
						{
							sqlParams[index].Value = System.Data.SqlTypes.SqlInt16.Parse(oMapping.Value);
						}
						break;
					case "int":
						sqlDataType = SqlDbType.Int;
						if (!string.IsNullOrEmpty(oMapping.Value))
						{
							sqlParams[index].Value = System.Data.SqlTypes.SqlInt32.Parse(oMapping.Value);
						}
						break;
					case "bigint":
						sqlDataType = SqlDbType.BigInt;
						if (!string.IsNullOrEmpty(oMapping.Value))
						{
							sqlParams[index].Value = System.Data.SqlTypes.SqlInt64.Parse(oMapping.Value);
						}
						break;
					case "nchar":
						sqlDataType = SqlDbType.NChar;
						sqlParams[index].Size = oMapping.Length;
						sqlParams[index].Value = oMapping.Value;
						break;
					case "char":
						sqlDataType = SqlDbType.Char;
						sqlParams[index].Size = oMapping.Length;
						sqlParams[index].Value = oMapping.Value;
						break;
					case "nvarchar":
						sqlDataType = SqlDbType.NVarChar;
						sqlParams[index].Size = oMapping.Length;
						sqlParams[index].Value = oMapping.Value;
						break;
					case "varchar":
						sqlDataType = SqlDbType.VarChar;
						sqlParams[index].Size = oMapping.Length;
						sqlParams[index].Value = oMapping.Value;
						break;
					case "ntext":
						sqlDataType = SqlDbType.NText;
						sqlParams[index].Value = oMapping.Value;
						break;
					case "text":
						sqlDataType = SqlDbType.Text;
						sqlParams[index].Value = oMapping.Value;
						break;
					case "float":
						sqlDataType = SqlDbType.Float;
						if (!string.IsNullOrEmpty(oMapping.Value))
						{
							sqlParams[index].Value = System.Data.SqlTypes.SqlDouble.Parse(oMapping.Value);
						}
						break;
					case "real":
						sqlDataType = SqlDbType.Real;
						if (!string.IsNullOrEmpty(oMapping.Value))
						{
							sqlParams[index].Value = System.Data.SqlTypes.SqlSingle.Parse(oMapping.Value);
						}
						break;
					case "money":
						sqlDataType = SqlDbType.Money;
						if (!string.IsNullOrEmpty(oMapping.Value))
						{
							sqlParams[index].Value = System.Data.SqlTypes.SqlMoney.Parse(oMapping.Value);
						}
						break;
					case "smallmoney":
						sqlDataType = SqlDbType.SmallMoney;
						if (!string.IsNullOrEmpty(oMapping.Value))
						{
							sqlParams[index].Value = System.Data.SqlTypes.SqlMoney.Parse(oMapping.Value);
						}
						break;
					case "datetime":
						sqlDataType = SqlDbType.DateTime;
						if (!string.IsNullOrEmpty(oMapping.Value))
						{
							sqlParams[index].Value = DateTime.Parse(oMapping.Value, userCulture);
						}
						break;
					case "datetime2":
						sqlDataType = SqlDbType.DateTime2;
						if (!string.IsNullOrEmpty(oMapping.Value))
						{
							sqlParams[index].Value = DateTime.Parse(oMapping.Value, userCulture);
						}
						break;
					case "smalldatetime":
						sqlDataType = SqlDbType.SmallDateTime;
						if (!string.IsNullOrEmpty(oMapping.Value))
						{
							sqlParams[index].Value = System.Data.SqlTypes.SqlDateTime.Parse(oMapping.Value);
						}
						break;
					case "bit":
						sqlDataType = SqlDbType.Bit;
						if (!string.IsNullOrEmpty(oMapping.Value))
						{
							sqlParams[index].Value = System.Data.SqlTypes.SqlBoolean.Parse(oMapping.Value);
						}
						break;
					case "uniqueidentifier":
						sqlDataType = SqlDbType.UniqueIdentifier;
						if (!string.IsNullOrEmpty(oMapping.Value))
						{
							sqlParams[index].Value = System.Data.SqlTypes.SqlGuid.Parse(oMapping.Value);
						}
						break;
					case "decimal":
						sqlDataType = SqlDbType.Decimal;
						if (!string.IsNullOrEmpty(oMapping.Value))
						{
							sqlParams[index].Value = System.Data.SqlTypes.SqlDecimal.Parse(oMapping.Value);
						}
						break;
					case "numeric":
						sqlDataType = SqlDbType.Decimal;
						if (!string.IsNullOrEmpty(oMapping.Value))
						{
							sqlParams[index].Value = System.Data.SqlTypes.SqlDecimal.Parse(oMapping.Value);
						}
						break;
				}
				
				if (oMapping.Nullable)
				{
					if (string.IsNullOrEmpty(oMapping.Value))
					{
						sqlParams[index].Value = System.DBNull.Value;
					}
				}
				
				sqlParams[index].ParameterName = "@" + oMapping.FormField;
				sqlParams[index].SqlDbType = sqlDataType;
				sqlParams[index].Direction = ParameterDirection.Input;
				sqlParams[index].Precision = oMapping.Prec;
				sqlParams[index].Scale = oMapping.Scale;
				sqlParams[index].IsNullable = oMapping.Nullable;
				sqlParams[index].SourceColumn = oMapping.TableField;
			}
			
			if (string.IsNullOrEmpty(connString))
			{
				connString = this.ConnectionString;
			}
			
			SqlHelper.ExecuteNonQuery(connString, CommandType.Text, sql, sqlParams);
			
		}
		
		public override void DeleteExternalTable(string sql, string connString)
		{
			if (string.IsNullOrEmpty(connString))
			{
				connString = this.ConnectionString;
			}
			
			SqlHelper.ExecuteNonQuery(connString, CommandType.Text, sql);
		}
		
		public override IDataReader GetFormDataToCopy(int moduleId, string idCsv)
		{
			return ((IDataReader) (SqlHelper.ExecuteReader(ConnectionString, DatabaseOwner + ObjectQualifier + "C5SLLC_GetFormDataToCopy", moduleId, idCsv)));
		}
		
		public override int GetFormDataToCopyCount(int moduleId, string idCsv)
		{
			return System.Convert.ToInt32(SqlHelper.ExecuteScalar(ConnectionString, DatabaseOwner + ObjectQualifier + "C5SLLC_GetFormDataToCopyCount", moduleId, idCsv));
		}
		
		public override void SetExtTableIdNull(int moduleId)
		{
			SqlHelper.ExecuteNonQuery(ConnectionString, DatabaseOwner + ObjectQualifier + "C5SLLC_FormSetExtTableIdNull", moduleId);
		}
		
		public override void SetPageExtTableIdNull(int formID, int pageNum)
		{
			SqlHelper.ExecuteNonQuery(ConnectionString, DatabaseOwner + ObjectQualifier + "C5SLLC_FormSetPageExtTableIdNull", formID, pageNum);
		}
		
#endregion
		
#region FormFileData Methods
		public override IDataReader GetFormFileData(int fileDataId)
		{
			return ((IDataReader) (SqlHelper.ExecuteReader(ConnectionString, DatabaseOwner + ObjectQualifier + "C5SLLC_FormFileDataGet", fileDataId)));
		}
		
		public override IDataReader ListFormFileData()
		{
			return ((IDataReader) (SqlHelper.ExecuteReader(ConnectionString, DatabaseOwner + ObjectQualifier + "C5SLLC_FormFileDataList")));
		}
		
		public override IDataReader GetFormFileDataByFormData(int formDataID)
		{
			return ((IDataReader) (SqlHelper.ExecuteReader(ConnectionString, DatabaseOwner + ObjectQualifier + "C5SLLC_FormFileDataGetByFormData", formDataID)));
		}
		
		public override IDataReader GetFormFileDataByControlId(int formDataId, string controlId)
		{
			return ((IDataReader) (SqlHelper.ExecuteReader(ConnectionString, DatabaseOwner + ObjectQualifier + "C5SLLC_FormFileDataGetByControlId", formDataId, controlId)));
		}
		
		public override int AddFormFileData(int formDataId, int portalId, int moduleId, string controlId, string contentType, int contentLegth, string filePath, string origFileName, string fileName, bool inDB, byte[] fileData, int createdByID, SqlTransaction objTransaction = null)
		{
			if (objTransaction == null)
			{
				return System.Convert.ToInt32(SqlHelper.ExecuteScalar(ConnectionString, DatabaseOwner + ObjectQualifier + "C5SLLC_FormFileDataAdd", GetNull(formDataId), portalId, moduleId, controlId, contentType, contentLegth, filePath, origFileName, fileName, inDB, GetNull(fileData), createdByID));
			}
			else
			{
				return System.Convert.ToInt32(SqlHelper.ExecuteScalar(objTransaction, DatabaseOwner + ObjectQualifier + "C5SLLC_FormFileDataAdd", GetNull(formDataId), portalId, moduleId, controlId, contentType, contentLegth, filePath, origFileName, fileName, inDB, GetNull(fileData), createdByID));
			}
			
		}
		
		public override void UpdateFormFileData(int fileDataId, int formDataId, string contentType, int contentLegth, string filePath, string origFileName, string fileName, bool inDB, byte[] fileData, int lastModifiedByID, SqlTransaction objTransaction = null)
		{
			if (objTransaction == null)
			{
				SqlHelper.ExecuteNonQuery(ConnectionString, DatabaseOwner + ObjectQualifier + "C5SLLC_FormFileDataUpdate", fileDataId, GetNull(formDataId), contentType, contentLegth, filePath, origFileName, fileName, inDB, GetNull(fileData), lastModifiedByID);
			}
			else
			{
				SqlHelper.ExecuteNonQuery(objTransaction, DatabaseOwner + ObjectQualifier + "C5SLLC_FormFileDataUpdate", fileDataId, GetNull(formDataId), contentType, contentLegth, filePath, origFileName, fileName, inDB, GetNull(fileData), lastModifiedByID);
			}
			
		}
		
		public override void DeleteFormFileData(int fileDataId, SqlTransaction objTransaction = null)
		{
			if (objTransaction == null)
			{
				SqlHelper.ExecuteNonQuery(ConnectionString, DatabaseOwner + ObjectQualifier + "C5SLLC_FormFileDataDelete", fileDataId);
			}
			else
			{
				SqlHelper.ExecuteNonQuery(objTransaction, DatabaseOwner + ObjectQualifier + "C5SLLC_FormFileDataDelete", fileDataId);
			}
			
		}
		
		public override IDataReader GetFilesByModuleID(int moduleId)
		{
			return ((IDataReader) (SqlHelper.ExecuteReader(ConnectionString, DatabaseOwner + ObjectQualifier + "C5SLLC_FormGetFilesByModuleId", moduleId)));
		}
		
		public override DataTable ExecuteSQL(string connString, string query)
		{
			DataTable dt = default(DataTable);
			
			if (connString == string.Empty)
			{
				connString = ConnectionString;
			}
			
			if (connString.ToLower().Contains("provider"))
			{
				// Create the connection object by using the preceding connection string.
				OleDbConnection objConn = new OleDbConnection(connString);
				
				// Open connection with the database.
				objConn.Open();
				
				// Create new OleDbCommand to return data from the file
				OleDbCommand objCmdSelect = new OleDbCommand(query, objConn);
				
				OleDbDataAdapter objAdapter = new OleDbDataAdapter();
				// Pass the Select command to the adapter.
				objAdapter.SelectCommand = objCmdSelect;
				
				DataSet objDataset = new DataSet();
				
				objAdapter.Fill(objDataset, "AdHocQuery");
				
				// Clean up objects.
				objConn.Close();
				
				dt = objDataset.Tables[0];
				
			}
			else
			{
				SqlDataReader dr = (SqlDataReader) ((IDataReader) (SqlHelper.ExecuteReader(connString, CommandType.Text, query)));
				if (dr != null)
				{
					dt = DotNetNuke.Common.Globals.ConvertDataReaderToDataTable(dr);
				}
				else
				{
					dt = new DataTable();
				}
				
			}
			
			
			return dt;
			
			
			//Return
			
		}
		
		public override DataSet ExecuteDataSet(string connString, string query)
		{
			DataSet ds = default(DataSet);
			
			if (connString == string.Empty)
			{
				connString = ConnectionString;
			}
			
			
			ds = SqlHelper.ExecuteDataset(connString, CommandType.Text, query);
			
			return ds;
			
		}
		
		public override IDataReader ExecuteDataReader(string connString, string query)
		{
			IDataReader dr = default(IDataReader);
			
			if (connString == string.Empty)
			{
				connString = ConnectionString;
			}
			
			
			dr = SqlHelper.ExecuteReader(connString, CommandType.Text, query);
			
			return dr;
			
		}
#endregion
		
#region FormData Web Service Methods
		public override IDataReader GetWsFormData(int formDataId, string moduleIds, DateTime dtStart, DateTime dtEnd)
		{
			return ((IDataReader) (SqlHelper.ExecuteReader(ConnectionString, DatabaseOwner + ObjectQualifier + "C5SLLC_FormGetWsFormData", formDataId, moduleIds, dtStart, dtEnd)));
		}
#endregion
		
#region FormMapping Methods
		
		
		public override IDataReader GetFormMappingByForm(int formID)
		{
			return ((IDataReader) (SqlHelper.ExecuteReader(ConnectionString, DatabaseOwner + ObjectQualifier + "C5SLLC_FormMappingGetByForm", formID)));
		}
		
		public override IDataReader GetFormMappingByPage(int formID, int pageNum)
		{
			return ((IDataReader) (SqlHelper.ExecuteReader(ConnectionString, DatabaseOwner + ObjectQualifier + "C5SLLC_FormMappingGetByPage", formID, pageNum)));
		}
		
		public override int AddFormMapping(int formID, string formField, string tableField, string dataType, int length, byte prec, byte scale, string nullable, int pageNum)
		{
			return System.Convert.ToInt32(SqlHelper.ExecuteScalar(ConnectionString, DatabaseOwner + ObjectQualifier + "C5SLLC_FormMappingAdd", formID, formField, tableField, dataType, length, GetNull(prec), GetNull(scale), nullable, GetNull(pageNum)));
		}
		
		public override void DeleteFormMappings(int formID)
		{
			SqlHelper.ExecuteNonQuery(ConnectionString, DatabaseOwner + ObjectQualifier + "C5SLLC_FormMappingsDelete", formID);
		}
		
		public override void DeleteFormMappingsByPage(int formID, int pageNum)
		{
			SqlHelper.ExecuteNonQuery(ConnectionString, DatabaseOwner + ObjectQualifier + "C5SLLC_FormMappingsDeleteByPage", formID, pageNum);
		}
#endregion
		
#region FormFieldRules Methods
		public override IDataReader GetFormFieldRulesByForm(int formID)
		{
			return ((IDataReader) (SqlHelper.ExecuteReader(ConnectionString, DatabaseOwner + ObjectQualifier + "C5SLLC_FormFieldRulesGetByForm", formID)));
		}
		
		public override int AddFormFieldRules(int formID, string sourceField, string sourceFieldName, string containsValue, byte fieldType, string formFields)
		{
			return System.Convert.ToInt32(SqlHelper.ExecuteScalar(ConnectionString, DatabaseOwner + ObjectQualifier + "C5SLLC_FormFieldRulesAdd", formID, sourceField, sourceFieldName, containsValue, fieldType, formFields));
		}
		
		public override void DeleteFormFieldRules(int fieldRuleID)
		{
			SqlHelper.ExecuteNonQuery(ConnectionString, DatabaseOwner + ObjectQualifier + "C5SLLC_FormFieldRulesDelete", fieldRuleID);
		}
		
		public override void DeleteFormFieldRulesByForm(int formID)
		{
			SqlHelper.ExecuteNonQuery(ConnectionString, DatabaseOwner + ObjectQualifier + "C5SLLC_FormFieldRulesDeleteByForm", formID);
		}
#endregion
		
#region FormPages Methods
		public override IDataReader GetFormPage(int formPageID)
		{
			return ((IDataReader) (SqlHelper.ExecuteReader(ConnectionString, GetFullyQualifiedName("C5SLLC_FormPageGet"), formPageID)));
		}
		
		public override IDataReader ListFormPages()
		{
			return ((IDataReader) (SqlHelper.ExecuteReader(ConnectionString, GetFullyQualifiedName("C5SLLC_FormPageList"))));
		}
		
		public override int AddFormPage(int formID, int pageNum, byte tableColumns, byte tableRows, byte labelPosition, string title, string shortName, string pageData, bool useTitle, string ExtIdentityColumn, string ExtTableConnStr, string ExtTableName, int createdByID, DateTime createdDate, int lastModifiedByID, DateTime lastModifiedDate)
		{
			
			return System.Convert.ToInt32(SqlHelper.ExecuteScalar(ConnectionString, GetFullyQualifiedName("C5SLLC_FormPageAdd"), formID, pageNum, tableColumns, tableRows, labelPosition, title, shortName, pageData, useTitle, ExtIdentityColumn, ExtTableConnStr, ExtTableName, createdByID, createdDate, lastModifiedByID, lastModifiedDate));
			
		}
		
		public override void UpdateFormPage(int formPageID, int formID, int pageNum, byte tableColumns, byte tableRows, byte labelPosition, string title, string shortName, string pageData, bool useTitle, string ExtIdentityColumn, string ExtTableConnStr, string ExtTableName, int createdByID, DateTime createdDate, int lastModifiedByID, DateTime lastModifiedDate)
		{
			
			SqlHelper.ExecuteNonQuery(ConnectionString, GetFullyQualifiedName("C5SLLC_FormPageUpdate"), formPageID, formID, pageNum, tableColumns, tableRows, labelPosition, title, shortName, pageData, useTitle, ExtIdentityColumn, ExtTableConnStr, ExtTableName, createdByID, createdDate, lastModifiedByID, lastModifiedDate);
			
		}
		
		public override void DeleteFormPage(int formPageID)
		{
			SqlHelper.ExecuteNonQuery(ConnectionString, GetFullyQualifiedName("C5SLLC_FormPageDelete"), formPageID);
		}
		
		public override void DeleteFormPageByFormID(int formID)
		{
			SqlHelper.ExecuteNonQuery(ConnectionString, GetFullyQualifiedName("C5SLLC_FormPageDeleteByFormID"), formID);
		}
		
		public override IDataReader GetFormPagesByFormID(int formID)
		{
			return ((IDataReader) (SqlHelper.ExecuteReader(ConnectionString, GetFullyQualifiedName("C5SLLC_FormPageGetByForm"), formID)));
		}
		
		public override void DeleteFormPageByPageNum(int formID, int pageNum)
		{
			SqlHelper.ExecuteNonQuery(ConnectionString, GetFullyQualifiedName("C5SLLC_FormPageDeleteByPageNum"), formID, pageNum);
		}
#endregion
		
#region Global Methods
		
		public override IDataReader GetGlobal(int globalID)
		{
			return ((IDataReader) (SqlHelper.ExecuteReader(ConnectionString, GetFullyQualifiedName("C5SLLC_GlobalGet"), globalID)));
		}
		
		public override IDataReader GetGlobalByKey(string itemKey)
		{
			return ((IDataReader) (SqlHelper.ExecuteReader(ConnectionString, GetFullyQualifiedName("C5SLLC_GlobalGetByKey"), itemKey)));
		}
		
		public override IDataReader ListGlobals()
		{
			return ((IDataReader) (SqlHelper.ExecuteReader(ConnectionString, GetFullyQualifiedName("C5SLLC_GlobalList"))));
		}
		
		public override int AddGlobal(string itemKey, string itemValue)
		{
			return System.Convert.ToInt32(SqlHelper.ExecuteScalar(ConnectionString, GetFullyQualifiedName("C5SLLC_GlobalAdd"), itemKey, itemValue));
		}
		
		public override void UpdateGlobal(int globalID, string itemKey, string itemValue)
		{
			SqlHelper.ExecuteNonQuery(ConnectionString, GetFullyQualifiedName("C5SLLC_GlobalUpdate"), globalID, itemKey, itemValue);
		}
		
		public override void DeleteGlobal(int globalID)
		{
			SqlHelper.ExecuteNonQuery(ConnectionString, GetFullyQualifiedName("C5SLLC_GlobalDelete"), globalID);
		}
		
#endregion
		
	}
	
}

